'use strict';

const Controller = require('egg').Controller;

class DoregisterController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, doregister';
    var { username, password } = ctx.request.body;
    var db = this.app.mongodb;
    const collection = await db.collection("user");
    var res = await collection.find({ username }).toArray();
    // console.log(res);
    if (res.length) {
      ctx.body = {
        status: 400,
        msg: "用户名已经被注册，请重新输入"
      }

    } else {
      var res = await collection.insertOne({ username, password })
      ctx.body = {
        status: 200,
        msg: "注册成功，请登陆"
      }

    }
  }
}

module.exports = DoregisterController;
